You are in charge of protecting a set of defenseless homeowners from predators that roam the area by constructing a fence of minimum length that encloses all of the homes.
The prototype for your solution is as follows:
typedef struct Node {
UInt32 nodeNum,
SInt32 xCoord;
SInt32 yCoord;
} Node;
void Perimeter(
UInt32 numHomes,
Node homesToEnclose[],
UInt32 *numNodesInPerimeter,
UInt32 nodesInPerimeter[]
);
You are given a list homesToEnclose of numHomes homes to protect by constructing a fence around the homes. You should return a list nodesInPerimeter of the numNodesInPerimeter homes to be connected by a fence. The fence must enclose all of the homes using the minimum amount of fencing material.
*)
unit Solution;
interface
// Do not modify the interface
uses
Types, Files;
type Node = record
xCoord: SInt32;
yCoord: SInt32;
end;
type NodeArray = array [0..0] of Node;
type UInt32Array = array [0..0] of UInt32;
procedure Perimeter( numHomes: UInt32; const homesToEnclose: NodeArray; var numNodesInPerimeter : UInt32; var nodesInPerimeter: UInt32Array);
implementation
// Fill in your solution and then submit this folder
// Team Name: Example Solution
// 45 minutes
function tabs( n: SInt32 ): SInt32;
begin
if n < 0 then begin
n := -n;
end;
tabs := n;
end;
function theta( const n1, n2: Node ): real;
var
dx, dy: SInt32;
ax, ay: SInt32;
t: real;
begin
dx := n2.xCoord - n1.xCoord;
dy := n2.yCoord - n1.yCoord;
ax := tabs(dx);
ay := tabs(dy);
if (dx = 0) & (dy=0) then begin
t := 0;
end else begin
t := dy/(ax+ay);
end;
if dx < 0 then begin
t := 2-t;
end else if dy < 0 then begin
t := 4 + t;
end;
theta := 90 * t;
end;
procedure Perimeter( numHomes: UInt32; const homesToEnclose: NodeArray; var numNodesInPerimeter : UInt32; var nodesInPerimeter: UInt32Array);
var
i: UInt32;
first, last, best: UInt32;
minangle, bestangle, thisangle: real;
begin
numNodesInPerimeter := 0;
if numHomes = 1 then begin
numNodesInPerimeter := 1;
nodesInPerimeter[0] := 0;
end else if numHomes >= 2 then begin
best := 0;
for i := 1 to numHomes-1 do begin
if homesToEnclose[i].yCoord < homesToEnclose[best].yCoord then begin